(function (PIXI, g, c, window) {
    'use strict';
	c.UIMache=function(j){
		PIXI.Container.call(this);
		this.j = j;
		Object.assign(this, {
			visible:false,
            tex_index : 0,
			txt_max : 400,
            txt : [],
			keyin:"",
        });
		
		this.position.set(g.app.view.width/2-200, g.app.view.height/2-150);
		this.bg = new PIXI.Graphics();
        this.bg.beginFill(0x000000, 0.5);
        this.bg.drawRect(0, 0, 400, 300);
        this.addChild(this.bg);
		this.button_clear = new c.Button(this.j);
        this.button_clear.setText("清空");
        this.button_clear.position.set(20, 270);
		this.button_cancel = new c.Button(this.j);
        this.button_cancel.setText("取消");
        this.button_cancel.position.set(120, 270);
		this.addCustom(this.button_clear);
		this.addCustom(this.button_cancel);
		for (var i = 0; i < this.txt_max; i++ ) {
            var tx = new PIXI.Text();
            this.addChild(tx);
            this.txt.push(tx);
        }
		
		this.buttons=[]
		for(var i=0;i<9;i++){
			var btn = new c.Button(this.j);
			btn.visible=false;
			this.keyin="";
			this.buttons.push(btn);
			this.addCustom(btn);
		}
	}
	c.UIMache.prototype = Object.create(PIXI.Container.prototype);
    c.UIMache.constructor = c.UIMache;
	c.UIMache.prototype.onPressedOK = function() {
		if(this.visible){
			if (this.button_cancel.state_ == this.button_cancel.state.Press) {
				this.keyin = "";
				this.button_cancel.reset();
				this.visible=false;
			}else if (this.button_clear.state_ == this.button_clear.state.Press) {
				this.keyin = "";
				this.button_clear.reset();
			}else{
				for(var i=0;i<this.buttons.length;i++){
					var btn = this.buttons[i];
					if(btn.state_==btn.state.Press && btn.visible && btn.func){
						this.keyin = "";
						btn.reset();
						this.visible=false;
						btn.func();
					}
				}
			}
		}
    };
	c.UIMache.prototype.draw=function(){
		this.tex_index = 0;
		if (this.txt) {
            for (var k in this.txt) {
                this.txt[k].visible = false;
            }
        }
		for(var i=0;i<this.buttons.length;i++){
			var btn = this.buttons[i];
			btn.visible=false;
		}
		
		var x=0;
		var y=0;
		var str = "江湖驿站为您服务，请输入地点";
		this.j.tm.rendertxt(str, 24, x, y, g.utils.rgbToHex(200, 150, 50), 0, this);
		y+=24;
		str=this.keyin;
		this.j.tm.rendertxt(str, 24, x, y, g.utils.rgbToHex(200, 200, 200), 0, this);

		var tmp_submap_infos=[];
		for(var i=0;i<this.j.save.submap_infos.length;i++){
			var can_enter = false;
			if (this.j.save.submap_infos[i].data.EntranceCondition == 0) {
				can_enter = true;
			}else if (this.j.save.submap_infos[i].data.EntranceCondition == 2){
				for (var ii in this.j.save.data.Team) {
					var r = this.j.save.data.Team[ii];
					if (this.j.save.getRole(r) && this.j.save.getRole(r).data.Speed >= 70) {
						can_enter = true;
						break;
					}
				}
			}
			var notIDs = [4,12,14,82];//昆仑仙境 高昌迷宫 明教分舵 梅庄地牢
			for(var j=0;j<=notIDs.length;j++){
				if(this.j.save.submap_infos[i].data.ID==notIDs[j]){
					can_enter=false;
					break;
				}
			}
			if(!can_enter)continue;
			var name = this.j.save.submap_infos[i].data.Name;
			var result = pinyinUtil.getPinyin(name, '', false, true);
			result = result.concat(pinyinUtil.getFirstLetter(name, true));
			for(var j=0;j<result.length;j++){
				var pinyin = result[j].toLowerCase();
				if(pinyin.indexOf(this.keyin)!=-1){
					tmp_submap_infos.push(this.j.save.submap_infos[i]);
					break;
				}
			}
			if(tmp_submap_infos.length==9){
				break;
			}
		}
		
		for(var i=0;i<tmp_submap_infos.length;i++){
			y+=24;
			var btn = this.buttons[i];
			btn.setText((i+1)+"."+tmp_submap_infos[i].data.Name);
			btn.position.set(x+10, y);
			btn.visible=true;
			btn.func=function(data){
				if(data.MainEntranceX2 && data.MainEntranceY2){
					this.j.mainmap.man_x=data.MainEntranceX2;
					this.j.mainmap.man_y=data.MainEntranceY2;
				}else{
					this.j.mainmap.man_x=data.MainEntranceX1;
					this.j.mainmap.man_y=data.MainEntranceY1;
				}
				this.j.mainmap.enterSubScene(data.ID,data.EntranceX,data.EntranceY);
			}.bind(this,tmp_submap_infos[i].data);
		}

		
	}
	c.UIMache.prototype.dealEvent=function(e){
		if(this.keyin.length<=20 && Array.from("qwertyuiopasdfghjklzxcvbnm").indexOf(e.key)!=-1){
			this.keyin+=e.key;
		}
		if(this.keyin.length>0 && e.keyCode==g.keyconfig.BACKSPACE){
			this.keyin=this.keyin.slice(0,-1);
		}
	}
})(PIXI, g, c, window);